from pymysql import Connection

adr = r"D:\Code\KnowCode\my_python\py_know.txt"
f = open(adr, 'r', encoding="UTF-8")
passwords = f.readline()

conn = Connection(
    host="localhost",  # 主机名
    port=3306,  # 端口
    user="root",  # 账户
    password=passwords,  # 密码
    # autocommit=True
)

# print(conn.get_server_info())

# 执行SQL语句
# 使用游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("test")
# 执行SQL
# cursor.execute("create table test_pymysql(id int);")  # 分号可以不写

# 执行带有查询性质的SQL语句
cursor.execute("select * from student")
# 使用fetchall()方法获取结果
result: tuple = cursor.fetchall()  # 返回类型是元组套元组
print(result)

# 对数据有更改的行为需要执行commit()确认才会生效
# 或在实例化Connection对象时传入autocommit=True
cursor.execute("insert into student values(8, '张三', 55)")
conn.commit()

# 关闭连接
conn.close()
